/*
 * Copyright (C) 2009 Leandro Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metalframework.persistence.repository.collection.jpa;

import java.util.*;

import org.metalframework.persistence.engine.*;
import org.slf4j.*;

/**
 * JPA-based persistent collection strategy supporting extent with
 * {@link org.metalframework.persistence.annotation.FetchStrategy#ALL}.
 * 
 * @param <T> Type of the entity whose strategy manipulates the extent.
 * 
 * @since 1.0
 * @see org.metalframework.persistence.annotation.FetchStrategy#ALL
 * @see org.metalframework.persistence.repository.collection.PersistentCollectionStrategy
 * @see org.metalframework.persistence.repository.collection.jpa.AbstractJpaPersistentCollectionStrategy
 * @see org.metalframework.persistence.repository.collection.jpa.JpaPersistentCollectionStrategyFactory
 */
public class FetchAllJpaPersistentCollectionStrategy<T> extends AbstractJpaPersistentCollectionStrategy<T> {
	
	private static final long serialVersionUID = 1135036404978303460L;

	private static final Logger logger = LoggerFactory.getLogger(FetchAllJpaPersistentCollectionStrategy.class);

	/**
	 * Entities retrieved from the JPA repository.
	 */
	private List<T> entities;
	
	/**
	 * Creates the strategy with the specified parameters.
	 * 
	 * @param entityType Type of the entity whose strategy manipulates the extent.
	 * @param jpaOperations Helper to execute JPA operations.
	 */
	public FetchAllJpaPersistentCollectionStrategy(Class<T> entityType, JapaEngine japaEngine) {
		super(entityType, japaEngine);
		fetch();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void doAdd(int index, T entity) {
		verifyBounds(index);
		entities.add(index, entity);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected T doSet(int index, T entity) {
		verifyBounds(index);
		return entities.set(index, entity);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected T doGet(int index) {
		verifyBounds(index);
		return entities.get(index);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected T doRemove(int index) {
		verifyBounds(index);
		return entities.remove(index);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean doRemove(T entity) {
		return entities.remove(entity);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean doContains(T entity) {
		return entities.contains(entity);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected int doSize() {
		return entities.size();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void flushed() {
		fetch();
	}

	/**
	 * Fetches all entities of type {@link #getEntityType()} with a single JPA SELECT query.
	 */
	private void fetch() {
		logger.debug("Fetching all entities of type [{}] from database", getEntityType().getName());
		List<T> list = getJapaEngine().query("SELECT c FROM " + getEntityType().getName() + " c");
		logger.debug("Found [{}] entities of type [{}]", list.size(), getEntityType().getName());
		
		this.entities = new ArrayList<T>(list);
	}

}
